package com.java.Lock.ReentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock可重入
 */
public class ReentrantLockReentrant {
    Lock lock = new ReentrantLock();

    public void func1() {
        try {
            lock.lock();
            System.out.println("房间1-钥匙A");
            // 中途带上钥匙离开房间的期间可进入到共用该钥匙的任意房间
            func2(); // 顺利进入func2，若非可重入则不能进入共用同一钥匙的其他房间
        } finally {
            lock.unlock();
        }
    }

    public void func2() {
        try {
            lock.lock();
            System.out.println("房间2-钥匙A");
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        new Thread(() -> {
            new ReentrantLockReentrant().func1();
        }).start();
    }
}
